<?php

namespace app\api\controller;

use app\common\business\User as UserBis;

class User extends AuthBase {

    /**
     * 获取用户数据
     */
    public function index() {
        $user = (new UserBis())->getNormalUserById($this->userId);
        $data = [
            'id'       => $this->userId,
            'username' => $user['username'],
            'sex'      => $user['sex']
        ];
        return show(config('status.success'), 'OK', $data);
    }

    /**
     * 更新用户数据
     */
    public function update() {
        $username = input('param.username', '', 'trim');
        $sex = input('param.sex', 0, 'intval');
        //
        $data = [
            'username' => $username,
            'sex'      => $sex
        ];
        $validate = (new \app\api\validate\User())->scene('update_user');
        if (!$validate->check($data)) {
            return show(config('status.error'), $validate->getError());
        }
        $userBisObj = new UserBis();
        $user = $userBisObj->update($this->userId, $data);
        if (!$user) {
            return show(config('status.error'), '更新失败');
        }
        // TODO 更新redis用户数据缓存
        return show(config('status.success'), 'ok');
    }
}